open source
Proprietary
In [12]:
import sqlite3
#driver is being imported
#psycopg2 for protsgeSQl
#pymysql for mySQL
In [13]:
conn= sqlite3.connect('example.sqlite3')
#if the give nname of file exits it load s the file else it creates the file
In [14]:
cur= conn.cursor()
#like pointer
In [15]:
cur.execute('CREATE TABLE countries(id integer, name text,iso3 text)')
#creates a table with columns id name and iso3
In [16]:
cur.execute('SELECT * FROM countries')
Out[16]:
In [10]:
cur.fetchall()
#data not entered in our table
Out[10]:
In [19]:
cur.execute('INSERT INTO countries(id,name,iso3)VALUES(1,"Nepal","NEP")')
Out[19]:
In [20]:
cur.execute('SELECT * FROM countries')
Out[20]:
In [21]:
cur.fetchall()
Out[21]:
In [22]:
sql='''INSERT INTO countries (id,name,iso3) VALUES (?,?,?)'''
cur.executemany(sql,[(2,'India','INA'),
(3,'Bhutan','BHU'),
(4,'Afghanistan','AFG')])
Out[22]:
In [23]:
cur.execute('SELECT * FROM countries')
Out[23]:
In [24]:
cur.fetchall()
Out[24]:
In [25]:
sql='''INSERT INTO countries (id,name,iso3) VALUES (4,'Pakistan','PAK')'''
cur.execute(sql)
Out[25]:
In [26]:
cur.execute('SELECT * FROM countries')
cur.fetchall()
Out[26]:
In [28]:
sql='UPDATE countries SET id=5 WHERE iso3= "PAK"'
cur.execute(sql)
Out[28]:
In [29]:
cur.execute('SELECT * FROM countries')
cur.fetchall()
Out[29]:
In [30]:
conn.commit()
#to write in the database
In [31]:
cur.execute('SELECT * FROM countries WHERE id=4')
cur.fetchall()
Out[31]:
In [32]:
cur.execute('SELECT * FROM countries WHERE id>3')
cur.fetchall()
Out[32]:
In [34]:
cur.execute('SELECT * FROM countries WHERE name LIKE "%an"')
cur.fetchall()
Out[34]:
In [35]:
cur.execute('SELECT * FROM countries WHERE name LIKE "%an%"')
cur.fetchall()
# last ma ra 1st ma j bhaye ni huncha
Out[35]:
In [36]:
cur.execute('SELECT * FROM countries WHERE name LIKE "an%"')
cur.fetchall()
# must start with an
Out[36]:
In [48]:
cur.execute('DELETE FROM countries')
cur.fetchall()
Out[48]:
In [ ]:
In [59]:
import csv
In [51]:
sql='INSERT INTO countries (id,name ,iso3) VALUES (?,?,?)'
_id= 1
with open('netdata.txt','r') as datafile:
csvfile=csv.DictReader(datafile)
for row in csvfile:
if row['Common Name'] and row['ISO 3166-1 3 Letter Code']:
cur.execute(sql, (_id, row['Common Name'], row['ISO 3166-1 3 Letter Code']))
_id+=1
conn.commit()
In [ ]:
In [52]:
cur.execute('SELECT * FROM countries')
cur.fetchall()
Out[52]:
In [70]:
cur.execute('DELETE FROM country_list')
cur.fetchall()
Out[70]:
In [71]:
sql= '''CREATE TABLE
country_list (id integer primary key autoincrement,
country_name text not null,
iso3 text not null unique)'''
cur.execute(sql)
In [72]:
sql='INSERT INTO country_list (country_name ,iso3) VALUES (?,?)'
with open('netdata.txt','r') as datafile:
csvfile=csv.DictReader(datafile)
for row in csvfile:
if row['Common Name'] and row['Formal Name']:
cur.execute(sql, (row['Common Name'], row['Formal Name']))
conn.commit()
In [73]:
cur.execute('SELECT * FROM country_list')
cur.fetchall()
Out[73]:
In [74]:
sql='''INSERT INTO country_list (id,country_name,iso3) VALUES (595, 'Reunion', 'Overseas Region of Reunion')'''
cur.execute(sql)
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: